/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package admin.unidad.usuario;

import DBMS.DataBaseManager;
import admin.permisos.PermisosManager;
import clases.Unidad;
import clases.Usuario;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

/**
 *
 * @author andreth
 */
public class ConsultarUsuario extends DispatchAction {

    private static final String SUCCESS = "success";
    private static final String FAILURE = "failure";
    private static final String verTodos = "verTodos";
    private static final String PAGE = "page";
    private static final String name = "Consultar Usuario";

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    public ActionForward all(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
 //        Usuario usr = (Usuario) request.getSession().getAttribute("logueado");
//        DataBaseManager dbms = DataBaseManager.getInstance();
//        if (!PermisosManager.validar(this.name, dbms.consultarPermisoAccion(name), usr.getPermiso())) {
//            System.out.println("No tiene permiso");
//            return mapping.findForward(FAILURE);
//        }
        DataBaseManager dbms = DataBaseManager.getInstance();
        ArrayList<Unidad> unidades = DataBaseManager.getInstance().consultarUnidades(new Unidad());
        ArrayList<Usuario> usuarios = (ArrayList<Usuario>) DataBaseManager.usuarios;
        System.out.print("ENTRANDO A ALL con USUARIOS EN NULL?" + (usuarios == null));
        request.setAttribute("usuarios", usuarios);
        if (usuarios.isEmpty()) {
            System.out.print("Problemas obteniendo todos los usuarios.");
            return mapping.findForward(FAILURE);
        }
        System.out.print("Retornando ver todos");
        return mapping.findForward(verTodos);
    }

    public ActionForward some(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
//        Usuario usr = (Usuario) request.getSession().getAttribute("logueado");
//        DataBaseManager dbms = DataBaseManager.getInstance();
//        if (!PermisosManager.validar(this.name, dbms.consultarPermisoAccion(name), usr.getPermiso())) {
//            System.out.println("No tiene permiso");
//            return mapping.findForward(FAILURE);
//        }
        DataBaseManager dbms = DataBaseManager.getInstance();
        ArrayList usuarios = DataBaseManager.getInstance().consultarUsuarios(((Usuario) form).getCategoria());
        request.setAttribute("usuarios", usuarios);
        if (usuarios == null || usuarios.isEmpty()) {
            return mapping.findForward(FAILURE);
        }
        ArrayList<Unidad> unidades = DataBaseManager.getInstance().consultarUnidades(new Unidad());
        System.out.print("Retornando consulta");
        return mapping.findForward(SUCCESS);
    }

    public ActionForward one(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
//        Usuario usr = (Usuario) request.getSession().getAttribute("logueado");
//        DataBaseManager dbms = DataBaseManager.getInstance();
//        if (!PermisosManager.validar(this.name, dbms.consultarPermisoAccion(name), usr.getPermiso())) {
//            System.out.println("No tiene permiso");
//            return mapping.findForward(FAILURE);
//        }
        DataBaseManager dbms = DataBaseManager.getInstance();
        Usuario usuario = (Usuario) form;
        usuario = DataBaseManager.getInstance().consultarUsuario(usuario);
        if (usuario != null) {
            ArrayList<Unidad> unidades = DataBaseManager.getInstance().consultarUnidades(new Unidad());
            request.setAttribute("usuario", usuario);
            System.out.print("Retornando ver uno");
            return mapping.findForward(SUCCESS);
        } else {
            return mapping.findForward(FAILURE);
        }
    }

    public ActionForward populate(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {
//        Usuario usr = (Usuario) request.getSession().getAttribute("logueado");
//        DataBaseManager dbms = DataBaseManager.getInstance();
//        if (!PermisosManager.validar(this.name, dbms.consultarPermisoAccion(name), usr.getPermiso())) {
//            System.out.println("No tiene permiso");
//            return mapping.findForward(FAILURE);
//        }
        DataBaseManager dbms = DataBaseManager.getInstance();
        System.out.print("Retornando la pagina");
        ArrayList<Unidad> unidades = DataBaseManager.getInstance().consultarUnidades(new Unidad());
        request.setAttribute("unidades", unidades);
        return mapping.findForward(PAGE);
    }
}
